﻿using AutoMapper;
using MediatR;
using SC.GirdManage.Common;
using SC.GirdManage.Common.Enums;
using SC.GirdManage.Repository;
using SC.GridManage.API.Write.Application.Command.LogicGrid;
using SC.GridManage.Domain.Models;

namespace SC.GridManage.API.Write.Application.Handler.LogicGrid
{
    public class DelLogicGridHandler : IRequestHandler<DelLogicGridCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<LogicGridModel> RepoLogic;
        private readonly IMapper mapper;

        public DelLogicGridHandler(IBaseRepository<LogicGridModel> repoLogic, IMapper mapper)
        {
            RepoLogic = repoLogic;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(DelLogicGridCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result= new ApiResult<int>();
            LogicGridModel logicGridModel = RepoLogic.QueryAsync().Result.Where(x => x.Id == request.Id).FirstOrDefault();
            if (logicGridModel==null)
            {
                result.Code=EnumResultCode.Fail;
                result.Msg = "你要删除的网格不存在";
                return Task.FromResult(result);
            }
            else
            {
                logicGridModel.IsDeleted=true;
              int r=  RepoLogic.UpdateAsync(logicGridModel).Result;
                if (r > 0)
                {
                    result.Code = EnumResultCode.Success;
                    result.Msg = "删除成功";
                    
                }
                else
                {
                    result.Code = EnumResultCode.Fail;
                    result.Msg = "删除失败";
                }
            }
            return Task.FromResult(result);
        }
    }
}
